package com.java.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.java.entity.ExAdmin;
import com.java.entity.ExChapter;
import com.java.entity.ExTopic;
import com.java.entity.vo.ExTopicVo;
import com.java.service.AccountService;
import com.java.service.AdminService;
import com.java.service.ChapterService;
import com.java.service.TopicService;
import com.java.util.MyCons;
import com.java.util.resp.BaseRespone;
import com.java.util.resp.TableRespone;
import com.java.util.servlet.ServletUtil;

@RestController
@RequestMapping("/admin")
public class AdminController {
	
	@Autowired private AdminService adminService;
	@Autowired private AccountService accountService;
	@Autowired private ChapterService chapterService;
	@Autowired private TopicService topicService;
	
	@PostMapping("/info")
	public BaseRespone info() {
		ExAdmin user = (ExAdmin) ServletUtil.getSession().getAttribute(MyCons.ADMIN_ROLE);
		ExAdmin admin = adminService.getAdmin(user.getAdminId());
		return BaseRespone.success(admin);
	}
	
	@PostMapping("/getAdmins")
	public List<ExAdmin> getAdmins() {
		return adminService.getAdmins();
	}
	
	@PostMapping("/saveAdmin")
	public BaseRespone saveAdmin(@RequestBody ExAdmin admin) {
		return adminService.save(admin);
	}
	
	@PostMapping("/delAdmin")
	public BaseRespone delAdmin(String adminId) {
		return adminService.delAdmin(adminId);
	}
	
	@PostMapping("/getAccounts")
	public TableRespone getAccounts(
			@RequestParam(required = false, defaultValue = "0") int page, 
			@RequestParam(required = false, defaultValue = "0") int size) {
		return accountService.getAccountList(page, size);
	}
	
	@PostMapping("/saveInfo")
	public BaseRespone saveInfo(@RequestBody ExAdmin admin) {
		return adminService.update(admin);
	}
	
	@PostMapping("/saveChapter")
	public BaseRespone saveChapter(@RequestBody ExChapter chapter) {
		return chapterService.saveChapter(chapter);
	}
	
	@PostMapping("/getChapters")
	public TableRespone getChapters(
			@RequestParam(required = false, defaultValue = "") Integer chapterType, 
			@RequestParam(required = false, defaultValue = "") String chapterTitle, 
			@RequestParam(required = false, defaultValue = "0") int page, 
			@RequestParam(required = false, defaultValue = "0") int size) {
		return chapterService.getChapters(chapterType, chapterTitle, page, size);
	}
	
	@PostMapping("/deleteChapter")
	public BaseRespone deleteChapter(String chapterId) {
		return chapterService.deleteChapter(chapterId);
	}
	
	@PostMapping("/deleteChapterItem")
	public BaseRespone deleteChapterItem(String chapterId, String topicId) {
		return chapterService.deleteChapterTopic(chapterId, topicId);
	}
	
	@PostMapping("/getChapterTopics")
	public TableRespone getTopics(
			@RequestParam(required = true) String chapterId, 
			@RequestParam(required = false, defaultValue = "0") int page, 
			@RequestParam(required = false, defaultValue = "0") int size) {
		return topicService.getChapterTopicVos(chapterId, page, size);
	}
	
	@PostMapping("/getTopicVo")
	public ExTopicVo getTopicVo(
			@RequestParam(required = true) String topicId) {
		return topicService.getTopicVo(topicId);
	}
	
	@PostMapping("/saveTopic")
	public BaseRespone saveTopic(@RequestParam(required = true) String chapterId,
			@RequestBody ExTopicVo topic) {
		return topicService.saveTopic(chapterId, topic);
	}
	
	@PostMapping("/updateTopic")
	public BaseRespone updateTopic(@RequestBody ExTopic topic) {
		return topicService.updateTopic(topic);
	}
	
	@PostMapping("/deleteTopic")
	public BaseRespone deleteTopic(
			@RequestParam(required = true) String chapterId, 
			@RequestParam(required = true) String topicId) {
		return topicService.deleteTopic(chapterId, topicId);
	}
	
	/*@PostMapping("/loadTopic")
	public BaseRespone loadTopic(String chapterId, Integer count) {
		return topicService.loadTopic(chapterId, count);
	}*/
	
	@PostMapping("/getRecords")
	public TableRespone getRecords(
			@RequestParam(required = true) String type, 
			@RequestParam(required = false) String title, 
			@RequestParam(required = false) String accountId, 
			@RequestParam(required = false, defaultValue = "0") int page, 
			@RequestParam(required = false, defaultValue = "0") int size) {
		return chapterService.getChapterRecords(title, type, accountId, page, size);
	}
	
	@PostMapping("/delAllChapter")
	public BaseRespone delAllChapter(@RequestBody List<String> list) {
		return chapterService.delAllChapter(list);
	}
	
	@PostMapping("/delAllTopic")
	public BaseRespone delAllTopic(@RequestBody List<String> list) {
		return topicService.delAllTopic(list);
	}
}
